package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yang
 * @date ${DATE}
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService  {

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    /**
     * 根据名称分页查询敏感词列表
     * @param sensitiveDto
     */
    @Override
    public ResponseResult sensitiveByPage(SensitiveDto sensitiveDto) {
        //1.校验参数
        if (sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.判断name是否为空 添加条件
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.lambdaQuery();
        if (Strings.isNotBlank(sensitiveDto.getName())){
            wrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        //封装分页参数
        IPage<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        //进行分页查询
        page(page,wrapper);

        //封装返回结果
        PageResponseResult result = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), page.getTotal(), page.getRecords());
        return result;
    }

    /**
     * 删除敏感词信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        //1.校验参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.查询这个敏感词是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, id));
        if (count == 0) {
            //说明该敏感词不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //3.根据id删除敏感词
        removeById(id);
        return ResponseResult.okResult();
    }

    /**
     * 保存敏感词信息
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        //注意:  id是自动生成的所以加不进去
        //1.校验敏感词参数
        if (adSensitive == null || Strings.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.判断敏感词是否已经存在
        int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0) {
            //说明敏感词已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在,不可重复添加");
        }
        //3.添加频道
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 修改敏感词信息
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //1. 校验参数
        if (adSensitive == null || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2 执行修改
        AdSensitive sensitive = getById(adSensitive.getId());
        if (sensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词信息不存在");
        }

        //3.校验名称唯一性
        //判断
        if (Strings.isNotBlank(sensitive.getSensitives()) && !sensitive.getSensitives().equals(adSensitive.getSensitives())) {
            //说明查询到的敏感词名称不为空,并且 查询到的敏感词和传递的名称不相同
            int count = count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getId, adSensitive.getId()));
            if (count == 0) {
                //说明敏感词不存在
                return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
            }
        }
        //说明频道存在 可以修改
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    /**
     * 查询所有敏感词
     * @return 敏感词集合
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
